# coding=utf-8

from appium import webdriver as appwebdriver
from selenium import webdriver as webdriver


class DriverTools():
    __driver = None
    __appdriver = None

    @classmethod
    def get_webdriver(cls):
        if cls.__driver is None:
            # 谷歌
            # cls.__driver = webdriver.Chrome()
            # 火狐
            cls.__driver = webdriver.Firefox()
            cls.__driver.maximize_window()
            cls.__driver.implicitly_wait(15)
        return cls.__driver

    @classmethod
    def get_appdriver(cls):
        if cls.__appdriver is None:
            desired_caps = dict()
            desired_caps["platformName"] = "Android"
            desired_caps["platformVersion"] = "9"
            desired_caps["deviceName"] = "MI 9"
            # self.desired_caps["automationName"] = "UIAutomator2"
            desired_caps["noReset"] = True
            desired_caps["appPackage"] = "com.microvirt.launcher2"
            desired_caps["appActivity"] = "com.microvirt.launcher.Launcher"
            # 解决中文输入异常
            desired_caps["unicodeKeyboard"] = True
            desired_caps["resetKeyboard"] = True
            # 连接到Appium服务器
            cls.__appdriver = appwebdriver.Remote(
                "http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
        return cls.__appdriver

    @classmethod
    # 定义退出appdriver
    def quit_app_driver(cls):
        if cls.__appdriver:
            cls.__appdriver.quit()
            cls.__appdriver = None

    @classmethod
    # 定义退出webdriver
    def quit_webdriver(cls):
        if cls.__driver:
            cls.__driver.quit()
            cls.__driver = None


if __name__ == "__main__":
    # 调试
    # DriverTools.get_webdriver().get("http://baidu.com")
    # DriverTools.quit_webdriver()
    DriverTools.get_appdriver()
    DriverTools.quit_app_driver()
